package com.yixiang.np.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yixiang.np.dao.LogDao;
import com.yixiang.np.model.PageFilter;
import com.yixiang.np.model.po.Log;
import com.yixiang.np.service.LogServiceI;



@Service
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class LogServiceImpl implements LogServiceI {

	@Autowired
	private LogDao logDao;


	@Override
	public List<Log> dataGrid(Log log, PageFilter ph) {
		List<Log> ul = new ArrayList<Log>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("whereSql", whereSql(log));
		params.put("orderSql", orderSql(ph));
		params.put("limitSql", limitSql(ph));
		ul = logDao.findAll(params);
		return ul;
	}

	@Override
	public Long count(Log role, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("whereSql", whereSql(role));
		params.put("orderSql", orderSql(ph));
		params.put("limitSql", limitSql(ph));
		Long number = logDao.count(params);
		return number;
	}
	
	private String whereSql(Log log) {
		String sql = "";
		if (log != null) {
			sql += " where 1=1 ";
			if (log.getUserid() != null) {
				sql += " and name like %" + log.getUserid() + "%";
			}
		}
		return sql;
	}
	private String orderSql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by " + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	private String limitSql(PageFilter ph) {
		int start = (ph.getPage()-1)*ph.getRows();
		String limitString  = " limit " + start + "," + ph.getRows();
		return limitString;
	}
	 
	@Override
	public void delete(Long id) {
		logDao.delete(id);
	}

	@Override
	public Log get(Long id) {
		return logDao.get(id);
	}
 
}
